<?php	defined('SYSPATH') or die('No direct script access.');
class Controller_Admin_Complaint extends Controller_Template
{

	protected $_limit	= '';
	protected $_complaint	= '';

	public function __construct($request, $response)
	{
	
		parent::__construct($request, $response);

		$this->_limit	= 20;
		$this->_complaint = new Model_Complaint();
		$this->template	= 'smarty:admin/complaint/index';
	}

	public function action_index($status = '')
	{

		$status	= intval($status);

		$arr_status		= '';
		if(empty($status))
			$arr_status	= array(1,2);//Kohana::config('complaint.status.wait_result'));
		else
			$arr_status	= array(3,4);
	
		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);

		//$status	= Kohana::config('complaint.status');	
		
		$datas	= $this->_complaint->get_statusdata($arr_status, $offset, $this->_limit);

		$count	= empty($datas) ? 0 : $datas['count'];
		
		$data	= empty($datas) ? array() : $this->format_data($datas['list']);
		
		$page_config	= array(
							'total_items'	=> $count,
							'items_per_page'=> $this->_limit,
							);
		
		$pagination		= Pagination::factory($page_config); 
		//print_r($data); 
		$this->template->set_filename('smarty:admin/complaint/list');
		$this->template->set('total', $count);
		$this->template->set('tstatus', $status);
		$this->template->set('function_name', 'complaint');
		$this->template->set('page_title', '投诉管理');
		$this->template->set('data', $data);
		$this->template->set('pagination', $pagination->render());
		$this->response->body($this->template->render());
	}

	public function action_detail($id)
	{
		
		$id = intval($id);

		if($id <= 0)
			return false;

		$data	= $this->_complaint->get_info_id($id);

		$datas	= $this->format_data($data);

		//print_r($datas);
		$this->template->set_filename('smarty:admin/complaint/detail');
		$this->template->set('data', empty($datas) ? null : $datas[0]);
		$this->template->set('order_id', $datas[0]['order_id']);
		$this->template->set('page_title', '投诉详情');
		$this->template->set('function_name', 'complaint');
		$this->response->body($this->template->render());
	}

	public function action_handle()
	{
		$id			= $this->request->post('id');

		$status		= $this->request->post('status');

		$content	= $this->request->post('content');
	
		$id			= intval($id);
		$status		= intval($status);
		$content	= strval(trim($content));
		
		if($id > 0 && $status > 0 && $content)
		{
			$rs			= $this->_complaint->set_status($id, $status, $content);

			$url		= '/complaint';

			echo '<script>alert("sucess"); window.location.href="'.$url.'"</script>';

		}else{

			$url        = '/complaint';

		    echo '<script>alert("fail"); window.location.href='.$url.'</script>';
		}
		exit;

	}

	protected function format_data($data)
	{
		for($i=0; $i < count($data); $i++)
		{
			$data[$i]['uname']	= User::name($data[$i]['uid']);
			
			if(empty($data[$i]['uname']))
			{
				$user_info			= User::search(array('uid' => $data[$i]['uid'], 'site' => 'member'), 0, 1);
				
				$data[$i]['uname']	= $user_info['list'][0]['passport'];
			}
			$data[$i]['create_time']	= date('Y-m-d', $data[$i]['create_time']);

			$data[$i]['update_time']	= date('Y-m-d', $data[$i]['update_time']);
		
			if($data[$i]['status'] == 1)
				$data[$i]['sname']	= '等待客服介入';
			if($data[$i]['status'] == 2)
				$data[$i]['sname']  = '客服已经介入';
			if($data[$i]['status'] == 3)
				$data[$i]['sname']  = '已解决-先行赔付';
			if($data[$i]['status'] == 4)
				$data[$i]['sname']  = '已解决-线下调节';
		}

		return $data;
	}
}


?>
